var Global = require("../util/global.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },
    onCollisionEnter(other,self) {

        var gameScript = self.node.parent.getChildByName('Canvas').getComponent("game");

        if(other.node.group=='bullet'){
            cc.log('我方炮弹');

            //移除子弹
            other.node.removeFromParent();
            
            //敌方减血
            gameScript.toBlood.progress -= 0.33;
            gameScript.toBloodTotal--;
            if(gameScript.toBloodTotal==0){
                gameScript.gameWin();
            }

            //判定是否胜利或失败
            if(self.bulletCount==3&&self.toBulletCount==3){
                if(self.bloodTotal>self.toBloodTotal){
                    message.type=88;
                    message.content="我方胜利";
                    self.gameWin();
                }else if(self.bloodTotal==self.toBloodTotal){
                    message.type=90;
                    message.content="双方平局";
                    self.gamePeace();
                }else{
                    message.type=89;
                    message.content="我方失败";
                    self.gameLose();
                }
                Global.socket.sendData(message);
            }

        }else if(other.node.group=='toBullet'){
            cc.log("敌方炮弹");

            //移除子弹
            other.node.removeFromParent();

            //我方减血
            gameScript.blood.progress -= 0.33;
            gameScript.bloodTotal--;
            if(gameScript.bloodTotal==0){
                gameScript.gameLose();
            }

            //发送答题错误
            message.type = 13;
            message.content = "";
            Global.socket.sendData(message);

            //关闭答题框
            gameScript.hideBulletPanel();
        }
    },

    // update (dt) {},
});
